<?php
declare (strict_types=1);

namespace app\api\controller\v3;


use app\common\model\ApiMember;
use app\common\model\Datum;
use app\common\model\Error as ErrorModel;
use app\common\model\Question;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use app\common\model\DatumCate;

class Error extends BaseController
{
    /**
     * 获取做错的题
     * @return \think\Response
     */
    public function mistake()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择标记id', []);
        }
        $model = new ErrorModel();
        if (!empty($model->where(['datum_id' => $id])->find())) {
            return errMsg(500, '请勿重复添加', []);
        }
        $data = Question::where(['id' => $id])
            ->field('id,title,name,grade_id,unit_id,mold_id,course_id,desc,answer,A,B,C,D')
            ->find()
            ->toArray();
        $data['api_member_id'] = $this->user_id;
        $data['datum_id'] = $id;
        $data['type'] = 2;
        unset($data['id']);
        $arr = $model->save($data);
        return sucessMsg(200, '加入成功', ['data' => $arr]);
    }

    /**
     * 获取不会做的题
     *
     * @return \think\Response
     */
    public function never()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择标记id', []);
        }
        $model = new ErrorModel();
        if (!empty($model->where(['datum_id' => $id])->find())) {
            return errMsg(500, '请勿重复添加', []);
        }
        //查询
        $data1 = Datum::where(['id' => $id])
            ->field('id,title,unit_id,datum_cate_id,English,American,video,desc,example,translate,else')
            ->find()->toArray();
        $data2 = DatumCate::where(['id' => $data1['datum_cate_id']])
            ->field('grade_id,mold_id,course_id,title')
            ->find()->toArray();
        //重命名
        $data2['name'] = $data2['title'];
        unset($data2['title']);

        $data1['api_member_id'] = $this->user_id;
        $data1['datum_id'] = $id;
        $data1['type'] = 1;
        unset($data1['id']);

        //合并
        $data = array_merge($data1, $data2);
//        dd($data);
        //存库
        $arr = $model->save($data);
        return sucessMsg(200, '加入成功', ['data' => $arr]);
    }

    //学习记录导出
    public function exportExcel()
    {
        // 查询要导出的数据
        $model = new ErrorModel();
        $data = $model
            ->field('id,api_member_id,title,Chinese,English,example,translate,desc')
            ->where('api_member_id', $this->user_id)
            ->select();
        foreach ($data as &$a) {
            $a['api_member_id'] = $a->apimember->nickname;
            unset($a->apimember);
        }
        // 实例化
        $spreadsheet = new Spreadsheet();
        // 获取活动单元格
        $sheet = $spreadsheet->getActiveSheet();

        // 获取单元格
        $cellA = $sheet->getCell('A1');
        // 设置单元格的值
        $cellA->setValue('ID');
        // 设置 A 列 列宽
        $sheet->getColumnDimension('A')->setWidth(10);
        // 设置第一行 行高
        $sheet->getRowDimension(1)->setRowHeight(20);

        $cellB = $sheet->getCell('B1');
        $cellB->setValue('记录人');
        $sheet->getColumnDimension('B')->setWidth(20);

        $cellC = $sheet->getCell('C1');
        $cellC->setValue('题目名称');
        $sheet->getColumnDimension('C')->setWidth(10);

        $cellD = $sheet->getCell('D1');
        $cellD->setValue('中文');
        $sheet->getColumnDimension('D')->setWidth(20);

        $cellE = $sheet->getCell('E1');
        $cellE->setValue('英文');
        $sheet->getColumnDimension('E')->setWidth(40);

        $cellE = $sheet->getCell('F1');
        $cellE->setValue('其他');
        $sheet->getColumnDimension('F')->setWidth(40);

        $cellF = $sheet->getCell('G1');
        $cellF->setValue('例句');
        $sheet->getColumnDimension('G')->setWidth(30);

        $cellG = $sheet->getCell('H1');
        $cellG->setValue('例句翻译');
        $sheet->getColumnDimension('H')->setWidth(30);

        $cellH = $sheet->getCell('I1');
        $cellH->setValue('注释');
        $sheet->getColumnDimension('I')->setWidth(30);


        // 设置样式 标题
        $styleArray = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '黑体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 设置样式 正文
        $styleArrayBody = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '宋体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 应用样式
        $sheet->getStyle('A1')->applyFromArray($styleArray);
        $sheet->getStyle('B1')->applyFromArray($styleArray);
        $sheet->getStyle('C1')->applyFromArray($styleArray);
        $sheet->getStyle('D1')->applyFromArray($styleArray);
        $sheet->getStyle('E1')->applyFromArray($styleArray);
        $sheet->getStyle('F1')->applyFromArray($styleArray);
        $sheet->getStyle('G1')->applyFromArray($styleArray);
        $sheet->getStyle('H1')->applyFromArray($styleArray);
        $sheet->getStyle('I1')->applyFromArray($styleArray);

        // 从 A2 开始填充数据
        foreach ($data as $k => $v) {
            $n = $k + 2;
            // 获取单元格
            $cellA = $sheet->getCell('A' . $n);
            // 设置单元格的值
            $cellA->setValue($v['id']);

            $cellB = $sheet->getCell('B' . $n);
            $cellB->setValue($v['api_member_id']);

            $cellC = $sheet->getCell('C' . $n);
            $cellC->setValue($v['title']);

            $cellD = $sheet->getCell('D' . $n);
            $cellD->setValue($v['Chinese']);

            $cellD = $sheet->getCell('E' . $n);
            $cellD->setValue($v['English']);

            $cellD = $sheet->getCell('F' . $n);
            $cellD->setValue($v['else']);

            $cellE = $sheet->getCell('G' . $n);
            $cellE->setValue($v['example']);

            $cellF = $sheet->getCell('H' . $n);
            $cellF->setValue($v['else']);

            $cellG = $sheet->getCell('I' . $n);
            $cellG->setValue($v['desc']);
        }


        $file_name = '学习记录列表.xlsx';
        // 实例化导出类
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=' . $file_name);
        header('Cache-Control:max-age=0');

        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

    //考试错题导出
    public function errorsExcel()
    {
        // 查询要导出的数据
        $model = new ErrorModel();
        $data = $model
            ->field('id,title,answer')
            ->where('api_member_id', $this->user_id)
            ->select();
        foreach ($data as &$a) {
            $a['api_member_id'] = $a->apimember->nickname;
            unset($a->apimember);
        }
        // 实例化
        $spreadsheet = new Spreadsheet();
        // 获取活动单元格
        $sheet = $spreadsheet->getActiveSheet();

        // 获取单元格
        $cellA = $sheet->getCell('A1');
        // 设置单元格的值
        $cellA->setValue('ID');
        // 设置 A 列 列宽
        $sheet->getColumnDimension('A')->setWidth(10);
        // 设置第一行 行高
        $sheet->getRowDimension(1)->setRowHeight(20);

        $cellB = $sheet->getCell('B1');
        $cellB->setValue('题目名称');
        $sheet->getColumnDimension('B')->setWidth(20);

        $cellC = $sheet->getCell('C1');
        $cellC->setValue('答案');
        $sheet->getColumnDimension('C')->setWidth(10);


        // 设置样式 标题
        $styleArray = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '黑体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 设置样式 正文
        $styleArrayBody = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
                'name' => '宋体',
                'bold' => false,
                'size' => 10
            ]
        ];
        // 应用样式
        $sheet->getStyle('A1')->applyFromArray($styleArray);
        $sheet->getStyle('B1')->applyFromArray($styleArray);
        $sheet->getStyle('C1')->applyFromArray($styleArray);


        // 从 A2 开始填充数据
        foreach ($data as $k => $v) {
            $n = $k + 2;
            // 获取单元格
            $cellA = $sheet->getCell('A' . $n);
            // 设置单元格的值
            $cellA->setValue($v['id']);

            $cellB = $sheet->getCell('B' . $n);
            $cellB->setValue($v['title']);

            $cellC = $sheet->getCell('C' . $n);
            $cellC->setValue($v['answer']);


            // 再给表格体设置样式
            $sheet->getStyle('A' . $n)->applyFromArray($styleArrayBody);
            $sheet->getStyle('B' . $n)->applyFromArray($styleArrayBody);
            $sheet->getStyle('C' . $n)->applyFromArray($styleArrayBody);

        }

        $file_name = '考试错题列表.xlsx';
        // 实例化导出类
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=' . $file_name);
        header('Cache-Control:max-age=0');

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }
}
